package com.slaffka;

import com.slaffka.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.Reader;

/**
 * Created by IntelliJ IDEA.
 * _User: v.tsapaev
 * Date: 11.02.12
 * Time: 12:13
 * To change this template use File | Settings | File Templates.
 */
public class ConnectionFactory {

    private static SqlSessionFactory sqlMapper;

    public static SqlSessionFactory getSqlSessionFactory() {
        if (sqlMapper == null){
            try {

                    String resource = "Configuration.xml";
                    Reader reader = Resources.getResourceAsReader(resource);
                    sqlMapper = new SqlSessionFactoryBuilder().build(reader);
                    initMapper();

            } catch (Exception e){
                e.printStackTrace();
            }
        }
        return sqlMapper;
    }

    private static void initMapper() {
        sqlMapper.getConfiguration().addMapper(UserMapper.class);
    }
}
